package com.twu.biblioteca;

import com.twu.biblioteca.controller.Biblioteca;
import com.twu.biblioteca.controller.Book;
import com.twu.biblioteca.controller.Movie;
import com.twu.biblioteca.controller.User;
import com.twu.biblioteca.model.UserDAO;

import java.util.ArrayList;
import java.util.Scanner;

public class BibliotecaApp {

    private UserDAO userDAO = new UserDAO();

    public static void main(String[] args) {
        new BibliotecaApp().run();
    }

    public String welcomeMessage() {
        return "Welcome to Biblioteca";
    }

    public int menu() {
        System.out.println("1 - List Books");
        System.out.println("2 - Checkout Book");
        System.out.println("3 - Return Book");
        System.out.println("4 - List Movies");
        System.out.println("5 - Checkout Movie");
        System.out.println("6 - Return Movie");
        System.out.println("7 - User Information");
        System.out.println("9 - Quit");
        do {
            try {
                Scanner scanner = new Scanner(System.in);
                int option = scanner.nextInt();
                if((option < 1 || option > 7) && option != 9)
                    throw new Exception();
                return option;
            } catch (Exception e) {
                System.out.println("Select a valid option!");
            }
        } while(true);
    }

    public void run() {
        Biblioteca biblioteca = new Biblioteca();

        System.out.println(welcomeMessage());

        do {
            int option = menu();
            switch (option) {
                case 1:
                    listBooks(biblioteca.listBooks());
                    break;
                case 2:
                    System.out.println(biblioteca.checkoutBook(askItemName(), login()));
                    break;
                case 3:
                    System.out.println(biblioteca.returnBook(askItemName()));
                    break;
                case 4:
                    login();
                    listMovies(biblioteca.listMovies());
                    break;
                case 5:
                    System.out.println(biblioteca.checkoutMovie(askItemName(), login()));
                    break;
                case 6:
                    login();
                    System.out.println(biblioteca.returnMovie(askItemName()));
                    break;
                case 7:
                    System.out.println(userInformation(login()));
                    break;
                case 9:
                    System.exit(0);
                    break;
                default:
                    break;
            }
        } while(true);
    }

    private void listBooks(ArrayList<Book> books) {
        for(Book book : books) {
            System.out.println(book.bookDetails());
        }
    }

    private void listMovies(ArrayList<Movie> movies) {
        for(Movie movie : movies) {
            System.out.println(movie.movieDetails());
        }
    }

    private String askItemName() {
        System.out.println("Input item name: ");
        Scanner scanner = new Scanner(System.in);
        return scanner.nextLine();
    }

    private String login() {
        Scanner scanner = new Scanner(System.in);
        User user;
        while(true) {
            System.out.println("Input user library number: ");
            String libraryNumber = scanner.nextLine();
            try {
                user = userDAO.findUserByLibraryNumber(libraryNumber);
                break;
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }

        while(true) {
            System.out.println("Input user password: ");
            if(user.isPasswordCorrect(scanner.nextLine()))
                break;
            else {
                System.out.println("Wrong password. Try again.");
            }
        }

        return user.getLibraryNumber();
    }

    private String userInformation(String LibraryNumber) {
        try {
            User user = userDAO.findUserByLibraryNumber(LibraryNumber);
            return "Name: " + user.getName() + " Email: " + user.getEmail() + " Phone: " + user.getPhone();
        } catch (Exception e) {
            return e.getMessage();
        }

    }
}